/*表单AJAX提交封装(包含验证)*/
function AjaxInitForm(formId, btnId, isDialog, urlId, callback){
    var formObj = $('#' + formId);
	var btnObj = $("#" + btnId);
	var urlObj = $("#" + urlId);
	var argNum = arguments.length; //参数个数
	formObj.Validform({
		tiptype:3,
		callback:function(form){
			//AJAX提交表单
            $(form).ajaxSubmit({
                beforeSubmit: formRequest,
                success: formResponse,
                error: formError,
                url: formObj.attr("url"),
                type: "post",
                dataType: "json",
                timeout: 60000
            });
            return false;
		}
	});
    
    //表单提交前
    function formRequest(formData, jqForm, options) {
        btnObj.prop("disabled", true);
        btnObj.val("提交中...");
    }

    //表单提交后
    function formResponse(data, textStatus) {
		if (data.status == 1) {
            btnObj.val("提交成功");
			//是否提示，默认不提示
			if(isDialog == 1){
				$.dialog.tips(data.msg, 2, "32X32/succ.png", function(){
					if (argNum == 5) {
						callback();
					}else if(data.url){
						location.href = data.url;
					}else if(urlObj.length > 0 && urlObj.val() != ""){
						location.href = urlObj.val();
					}else{
						location.reload();
					}
				});
			}else{
				if (argNum == 5) {
					callback();
				}else if(data.url){
					location.href = data.url;
				}else if(urlObj){
					location.href = urlObj.val();
				}else{
					location.reload();
				}
			}
        } else {
            $.dialog.alert(data.msg);
            btnObj.prop("disabled", false);
            btnObj.val("再次提交");
        }
    }
    //表单提交出错
    function formError(XMLHttpRequest, textStatus, errorThrown) {
		$.dialog.alert("状态：" + textStatus + "；出错提示：" + errorThrown);
        btnObj.prop("disabled", false);
        btnObj.val("再次提交");
    }
}
//单击执行AJAX请求操作
function clickSubmit(sendUrl){
	$.ajax({
		type: "POST",
		url: sendUrl,
		dataType: "json",
		timeout: 20000,
		success: function(data, textStatus) {
			if (data.status == 1){
				$.dialog.tips(data.msg, 2, "32X32/succ.png", function(){
					location.reload();
			    });
			} else {
				$.dialog.alert(data.msg);
			}
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			$.dialog.alert("状态：" + textStatus + "；出错提示：" + errorThrown);
		}
	});
}
/*AJAX加载评论列表*/
var comment_page_index = 1; //评论页码全局变量
function CommentAjaxList(btnObj,listDiv,pageSize, pageCount, sendUrl){
	//计算总页数
	
	var pageTotal = Math.ceil(pageCount / pageSize);
	if(comment_page_index > pageTotal){
		return false;
	}
	//发送AJAX请求
	$.ajax({
		type:"post",
		url:"http://localhost:810/tools/submit_ajax.ashx?action=comment_list&article_id=76&page_size=10&page_index=2",
		dataType:"json",
		beforeSend:function(){
			$(btnObj).prop("disabled", true);
			$(btnObj).val('正在加载...');
		},
		success:function(data){
			var strHtml = '';
			for(var i in data){
				strHtml += '<li>' + 
				'<div class="inner">' +
				'<p>这是个内容</p>' +
				'<div class="meta">' +
				'<span class="blue">这是用户名</span>\n' +
				'<span class="time">这是个时间</span>' +
				'</div>' +
				'</div>';
				strHtml += '</li>';
			}
			//添加到列表
			if(comment_page_index==1){
				$(listDiv).html(strHtml);
			}else{
				$(listDiv).append(strHtml);
			}
			comment_page_index++; //当前页码加一
			if(comment_page_index > pageTotal){
				$(btnObj).hide();
			}
		},
		complete:function(){
			$(btnObj).prop("disabled", false);
			$(btnObj).val('加载更多评论');
		}
	});
}